package Conexao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Conexao{

    private static Conexao conexao = null;
    private Connection con;
    private PreparedStatement ps;

    public PreparedStatement getPs() {
        return ps;
    }
    
    private Conexao() {        
        try {
            String driverName = "com.mysql.jdbc.Driver"; // MySQL MM JDBC driver
            Class.forName(driverName).newInstance();
            
            String server = "localhost";
            String database = "loginjsp";
            String usuario = "root";
            String senha = "deldenis";
            
            this.con = DriverManager.
                    getConnection("jdbc:mysql://" + server + "/" + database,
                            usuario, senha);           
            
        }catch (InstantiationException ex)
        {
          Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex)
        {
          Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException e)
        {
         System.out.println("Não foi possível encontrar o driver: " + e.toString());
        } catch (java.sql.SQLException e)
        {
          System.out.println("Não foi possível conectar ao banco de dados: " + e.toString());
        }
    }

    public static Conexao getInstancia() {
        if (getConexao() == null) {
            setConexao(new Conexao());
        }
        return (getConexao());
    }

    public void preparar(String sql) {
        try {
            this.ps = this.con.prepareStatement(sql);
        } catch (SQLException e) {
         
            e.printStackTrace();
        }
    }

    public void prepararAI(String sql) {
        try {
            this.ps = this.con.prepareStatement(sql,
                    Statement.RETURN_GENERATED_KEYS);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public int getAutoIncrement() {
        try {
            ResultSet rs = this.ps.getGeneratedKeys();
            if (rs.next()) {
                return (rs.getInt(1));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return (0);
    }

    public void fechar() {
        try {
            this.ps.close();
            this.con.close();
            Conexao.setConexao(null);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public ResultSet executeQuery() {
        try {
            return (this.ps.executeQuery());
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public boolean executeUpdate() {
        try {
            return (this.ps.executeUpdate() > 0);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return (false);
    }

    public static Conexao getConexao() {
        return conexao;
    }

    public static void setConexao(Conexao conexao) {
        Conexao.conexao = conexao;
    }

}
